package com.lrkj.waitress.service.impl;

import java.util.List;

import com.lrkj.common.annotation.DataScope;
import com.lrkj.common.constant.FeeConstants;
import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.framework.utils.NumberUtils;
import com.lrkj.framework.utils.SerialNumUtil;
import com.lrkj.property.domain.Community;
import com.lrkj.property.domain.Room;
import com.lrkj.property.service.ICommunityService;
import com.lrkj.property.service.IRoomService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.waitress.mapper.RepairsMapper;
import com.lrkj.waitress.domain.Repairs;
import com.lrkj.waitress.service.IRepairsService;

/**
 * 投诉建议Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-22
 */
@Service
public class RepairsServiceImpl implements IRepairsService {
    @Autowired
    private RepairsMapper repairsMapper;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private ICommunityService communityService;

    /**
     * 查询投诉建议
     *
     * @param id 投诉建议ID
     * @return 投诉建议
     */
    @Override
    public Repairs selectRepairsById(String id) {
        return repairsMapper.selectRepairsById(id);
    }

    /**
     * 查询投诉建议列表
     *
     * @param repairs 投诉建议
     * @return 投诉建议
     */
    @Override
    @DataScope(deptAlias = "t")
    public List<Repairs> selectRepairsList(Repairs repairs) {
        return repairsMapper.selectRepairsList(repairs);
    }

    /**
     * 新增投诉建议
     *
     * @param repairs 投诉建议
     * @return 结果
     */
    @Override
    public int insertRepairs(Repairs repairs) {
        repairs.setId(IdUtils.simpleUUID());
        repairs.setCreateDate(DateUtils.getNowDate());
        repairs.setRepairsOdd(this.getNo(FeeConstants.repairs_odd));
        if (!repairs.getServiceType().equals("wx")) {
            this.setUnitInfo(repairs);
        }
        return repairsMapper.insertRepairs(repairs);
    }

    /**
     * 修改投诉建议
     *
     * @param repairs 投诉建议
     * @return 结果
     */
    @Override
    public int updateRepairs(Repairs repairs) {
        this.setUnitInfo(repairs);
        repairs.setUpdateBy(SecurityUtils.getUsername());
        repairs.setUpdateDate(DateUtils.getNowDate());
        return repairsMapper.updateRepairs(repairs);
    }

    /**
     * 批量删除投诉建议
     *
     * @param ids 需要删除的投诉建议ID
     * @return 结果
     */
    @Override
    public int deleteRepairsByIds(String[] ids) {
        orgTreeService.deleteOrgTreeByIds(ids);
        return repairsMapper.deleteRepairsByIds(ids);
    }

    /**
     * 删除投诉建议信息
     *
     * @param id 投诉建议ID
     * @return 结果
     */
    @Override
    public int deleteRepairsById(String id) {
        orgTreeService.deleteOrgTreeById(id);
        return repairsMapper.deleteRepairsById(id);
    }


    private void setUnitInfo(Repairs bean) {
        Room parentBean = roomService.selectRoomById(bean.getRoomId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getRoomId());
        bean.setDeptId(parentBean.getDeptId());
        orgTreeService.insertOrgTree(bean.getId(), bean.getRepairsPerson(), bean.getRoomId(), OrgConstants.repairs, bean.getAncestors());
    }

    public String getNo(String key) {
        int nextVal = SerialNumUtil.getInstance().nextInt(key);
        return key + NumberUtils.frontCompWithZore(nextVal, 7);
    }

}
